﻿using DbControl;
using Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CentralMonitoring
{
    /// <summary>
    /// 设备参数配置窗体
    /// </summary>
    public partial class SensorEdit : Form
    {    
        public int m_Id = -1;       
        public OprType m_type = OprType.Add;
        public string m_beforName = "";
        private string m_beforCode = "";
        private sensors m_devices;
        public SensorEdit()
        {
            InitializeComponent();
        }
        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBoxName.Text) || textBoxName.Text.Length < 1)
            {
                MessageBox.Show("名称为必填项！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (string.IsNullOrEmpty(textBoxName.Text) || textBoxName.Text.Length < 1)
            {
                MessageBox.Show("编码为识别码与终端绑定,为必填项！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }           
            if (m_type == OprType.Add)
            {
                sensors dv = new sensors();
                if (!string.IsNullOrEmpty(textBoxCode.Text))
                    dv.sensor_code = textBoxCode.Text;
                dv.sensor_name = textBoxName.Text;
                dv.sensor_sn = textBoxSN.Text;
                if (!string.IsNullOrEmpty(textBoxtype.Text))
                    dv.sensor_type = textBoxtype.Text;
                if (!string.IsNullOrEmpty(textBoxmode.Text))
                    dv.sensor_mode = textBoxmode.Text;
                if (!string.IsNullOrEmpty(textBoxTopic.Text))
                    dv.sensor_topic = textBoxTopic.Text;
                if(!string.IsNullOrEmpty(textBoxSubscribe.Text))
                    dv.sensor_subscribe =textBoxSubscribe.Text;
                dv.flag = comboBoxFlag.SelectedIndex;               
                if (OprSensor.IsExistSensorName(textBoxName.Text))
                {
                    MessageBox.Show("名称已存在！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;

                }
                if (OprSensor.IsExistSensorCode(textBoxCode.Text))
                {
                    MessageBox.Show("识别码已存在！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                try
                {
                    OprSensor.Insert(dv);
                    this.DialogResult = DialogResult.OK;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("操作失败！" + ex.Message.ToString());
                } 
            }
            else
            {
                if (OprSensor.IsExistSensorName(textBoxName.Text) && m_beforName != textBoxName.Text)
                {
                    MessageBox.Show("名称已存在！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                if (!string.IsNullOrEmpty(m_beforCode))
                {
                    if (OprSensor.IsExistSensorCode(textBoxCode.Text) && m_beforCode != textBoxCode.Text)
                    {
                        MessageBox.Show("编码已存在！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                }
                try
                {                    
                    if (!string.IsNullOrEmpty(textBoxCode.Text))
                        m_devices.sensor_code = textBoxCode.Text;
                    m_devices.sensor_name = textBoxName.Text;
                    m_devices.sensor_sn = textBoxSN.Text;
                    if (!string.IsNullOrEmpty(textBoxtype.Text))
                        m_devices.sensor_type = textBoxtype.Text;
                    if (!string.IsNullOrEmpty(textBoxmode.Text))
                        m_devices.sensor_mode = textBoxmode.Text;
                    if (!string.IsNullOrEmpty(textBoxTopic.Text))
                        m_devices.sensor_topic = textBoxTopic.Text;
                    if (!string.IsNullOrEmpty(textBoxSubscribe.Text))
                        m_devices.sensor_subscribe = textBoxSubscribe.Text;
                    m_devices.flag = comboBoxFlag.SelectedIndex;                 
                    OprSensor.Update(m_devices);
                    this.DialogResult = DialogResult.OK;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("操作失败！" + ex.ToString());
                }
            }
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void SensorEdit_Load(object sender, EventArgs e)
        {
            if (m_Id > 0)
            {
                m_devices = OprSensor.GetSensorById(m_Id);
                if (m_devices != null)
                {
                    
                    textBoxCode.Text = m_devices.sensor_code;
                    textBoxName.Text = m_devices.sensor_name;
                    textBoxtype.Text = m_devices.sensor_type;
                    textBoxSN.Text = m_devices.sensor_sn;
                    if(!string.IsNullOrEmpty(m_devices.sensor_sn))
                    {
                        textBoxSN.Text = m_devices.sensor_sn;
                    }                        
                    if(!string.IsNullOrEmpty(m_devices.sensor_type))
                    {
                        textBoxtype.Text = m_devices.sensor_type;
                    }                        
                    if(!string.IsNullOrEmpty(m_devices.sensor_mode))
                    {
                        textBoxmode.Text = m_devices.sensor_mode;
                    }
                    if(!string.IsNullOrEmpty(m_devices.sensor_topic))
                    {
                        textBoxTopic.Text = m_devices.sensor_topic;
                    }
                    if(!string.IsNullOrEmpty(m_devices.sensor_subscribe))
                    {
                        textBoxSubscribe.Text = m_devices.sensor_subscribe;
                    }                 
                    if(m_devices.flag.HasValue)
                    {
                        comboBoxFlag.SelectedIndex = m_devices.flag.Value;
                    }                   
                }
            }
        }
    }
}
